package com.text.zzj;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerThread implements Runnable {
	private int serverPort;
	private ServerSocket serverS;

	public ServerThread(int serverPort) {
		super();
		this.serverPort = serverPort;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			serverS = new ServerSocket(serverPort);
			while (true) {
				Socket socket = serverS.accept();
				BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
				// byte[] b = new byte[1024];
				// int len = -1;
				String strLine = null;
				while (true) {
					strLine = br.readLine();

					// 结束读取内容
					if (strLine.equals("eof") && strLine != null) {
						System.out.println("服务器收到消息" + strLine);

					} else {
						break;
					}

				}

				PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "utf-8"));
				String strO = "当然是选择原谅他...";
				// byte[] bo = strO.getBytes();
				pw.println(strO);
				pw.print("eof");
				pw.flush();
				System.out.println("服务器发送消息：" + strO);

				// 结束资源
				br.close();
				pw.close();
				socket.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
